package com.xinsoft.controller.device;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.CusDeviceSpareDTO;
import com.xinsoft.entity.dto.CusDeviceSparePageParam;
import com.xinsoft.entity.dto.ErpRealTimeDto;
import com.xinsoft.entity.dto.ErpRealTimePageParam;
import com.xinsoft.entity.vo.CusDeviceSpareVo;
import com.xinsoft.service.CusDeviceSpareService;
import com.xinsoft.service.impl.SyncPullBillAdapter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/cus/deviceSpare")
public class CusDeviceSpareController {

    @Autowired
    private CusDeviceSpareService cusDeviceService;

    @Resource(name = "syncPullBillAdapter")
    private SyncPullBillAdapter syncPullBillAdapter;

    /**
     * 新增
     */
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> add(@RequestBody CusDeviceSpareDTO cusDeviceSpareDTO) {
        cusDeviceService.addCusDeviceSpareList(cusDeviceSpareDTO);
        return Response.succeed();
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> del(@RequestBody List<Integer> ids) {
        cusDeviceService.del(ids);
        return Response.succeed();
    }

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody CusDeviceSparePageParam cusDevicePageParam) {
        IPage<CusDeviceSpareVo> iPage = cusDeviceService.findPageListByParam(cusDevicePageParam);
        List<CusDeviceSpareVo> records = iPage.getRecords();
        if (ObjectUtil.isEmpty(records))  return Response.succeed(iPage);

        //填充实时库存
        List<Integer> materialIds = records.stream().map(CusDeviceSpareVo::getMaterialId).collect(Collectors.toList());
        ErpRealTimePageParam realTimePageParam = new ErpRealTimePageParam();
        realTimePageParam.setMaterialIds("(" + StringUtils.join(materialIds, ",") + ")");
        IPage<ErpRealTimeDto> realTimeDtoIPage = syncPullBillAdapter.realtimeInventory(realTimePageParam);
        List<ErpRealTimeDto> realTimeDtos = realTimeDtoIPage.getRecords();
        Map<Integer, List<ErpRealTimeDto>> realTimeDtosGroup = realTimeDtos.stream().collect(Collectors.groupingBy(ErpRealTimeDto::getMaterialId));
        records.forEach(item -> {
            if (!realTimeDtosGroup.containsKey(item.getMaterialId())) return;
            List<ErpRealTimeDto> realTimeDtosOfMaterial = realTimeDtosGroup.get(item.getMaterialId());
            BigDecimal reduce = realTimeDtosOfMaterial.stream().map(item2 -> new BigDecimal(item2.getNum().toString()))
                    .reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
            item.setTotalStockNum(reduce);
        });
        return Response.succeed(iPage);
    }
}
